//
//  QJShopDetailOneVC.m
//  QuanJieShopProject
//
//  Created by apple on 2018/1/31.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "QJShopDetailOneVC.h"
#import "QJDetailVCHeaderOne.h"
#import "YFShopDetailCell.h"
#import "YFGoodsTypeView.h"

@interface QJShopDetailOneVC () <UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *oneTableView;
@property (nonatomic, strong) QJDetailVCHeaderOne *headerView;


//@property (nonatomic, strong) YFGoodsTypeView *typeView;

@end

@implementation QJShopDetailOneVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataSourceArr = [NSMutableArray array];
    self.buyCount = 1;
    [self setupOneUIAction];
}
- (void)setupOneUIAction{
    self.view.backgroundColor = YFColor_Background;
    WEAKSELF
    self.headerView = [[[NSBundle mainBundle]loadNibNamed:@"QJDetailVCHeaderOne" owner:self options:nil] lastObject];
    [self.headerView headerUIPrepareAction];
//    NSLog(@"self.headerView.mj_h = %f", self.headerView.mj_h);
//    NSLog(@"UISCREEN_WIDTH + 150 = %f", UISCREEN_WIDTH + 150);

    self.headerView.changeShopNumberBlock = ^(NSInteger num) {
        if (weakSelf.buyShopNumBlock) {
            weakSelf.buyShopNumBlock(num);
        }
    };
    
    CGFloat bottomViewH = 100.f/1334.f * UISCREEN_HEIGHT;

    // 52是自定义导航栏的高度
    self.oneTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UISCREEN_WIDTH, UISCREEN_HEIGHT - bottomViewH - Height_NavBar) style:UITableViewStylePlain];
    [self.view addSubview:self.oneTableView];
    self.oneTableView.tableHeaderView = self.headerView;
    self.oneTableView.delegate = self;
    self.oneTableView.dataSource = self;
    self.oneTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    self.oneTableView.backgroundColor = [UIColor blueColor];
    
}
- (void)detailOneUpdateWithDic:(NSMutableDictionary *)dict{
    
    // 规格
    NSDictionary *data = dict[@"data"];
    
    // 规格
//    NSArray *typeArr = data[@"filter_spec"];
//    NSMutableArray *tempTypeArr = [NSMutableArray array];
//    for (NSDictionary *dict in typeArr) {
//        // 外层model
//        YFGoodsTypeModel *model = [YFGoodsTypeModel alloc];
//        model.title = dict[@"name"];
//
//        NSArray *subArr = dict[@"item"];
//
//        NSMutableArray *tempArr = [NSMutableArray array];
//        for (NSDictionary *subDict in subArr) {
//            // 商品类型选择
//            YFGoodsTypeSubModel *subModel = [[YFGoodsTypeSubModel alloc] initWithDictionary:subDict error:nil];
//            subModel.state = @1; // 默认都是一般状态
//            [tempArr addObject:subModel];
//        }
//        model.dataArr = tempArr;
//
//        [tempTypeArr addObject:model];
//    }
//    self.typeView.goodsTypeArr = [NSArray arrayWithArray:tempTypeArr];

    
    // 不同规格组合,不同价格
//    self.typeView.goodsPriceArr = nil;
//    NSArray *goodsPriceArr = data[@"spec_goods_price"];
//    NSMutableArray *tempPriceArr = [NSMutableArray array];
//    if (goodsPriceArr.count) {
//        for (NSDictionary *dict in goodsPriceArr) {
//            YFGoodsPriceModel *model = [[YFGoodsPriceModel alloc] initWithDictionary:dict error:nil];
//            [tempPriceArr addObject:model];
//        }
//    }
//    self.typeView.goodsPriceArr = [NSArray arrayWithArray:tempPriceArr];
    
    // 头部信息
    [self.headerView headerUpdateWithDic:dict];
    
    // cell显示
    [self.dataSourceArr removeAllObjects];
    // 默认的第一个写死数据
    YFShopDetailCellModel *model = [[YFShopDetailCellModel alloc] init];
    model.title = @"服务";
    model.datail = @"品质保证 7天无理由退换 极速发货 担保交易";
    model.isShowSelectImg = NO;
    [self.dataSourceArr addObject:model];
    
    NSArray *goodsTypeArr = dict[@"data"][@"filter_spec"];
    
    if (goodsTypeArr.count) {
        YFShopDetailCellModel *model2 = [[YFShopDetailCellModel alloc] init];
        model2.title = @"规格";
        NSString *detailStr = @"选择   ";
        for (int i =0; i<goodsTypeArr.count; i++) {
            NSDictionary *dict = goodsTypeArr[i];
            NSString *title = dict[@"name"];
            if (i == 0) {
                detailStr = [detailStr stringByAppendingString:title];
            }else {
                detailStr = [detailStr stringByAppendingString:[NSString stringWithFormat:@",%@", title]];
            }
        }
        model2.datail = detailStr;
        [self.dataSourceArr addObject:model2];
//        NSLog(@"detailStr = %@", detailStr);
    }
    
    [self.oneTableView reloadData];
}
#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSLog(@"dataSourceArr = %@", self.dataSourceArr);
    return self.dataSourceArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    YFShopDetailCell *cell = [YFShopDetailCell cellWithTableView:tableView];
    YFShopDetailCellModel *model = self.dataSourceArr[indexPath.row];
    cell.titleLabel.text = model.title;
    cell.detailLabel.text = model.datail;
    if (indexPath.row == 0) {
        cell.selectImgView.hidden = YES;
        cell.titleLabel.numberOfLines = 2;
    }else {
        cell.selectImgView.hidden = NO;
        cell.titleLabel.numberOfLines = 1;
    }
    [cell updateConstraints];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.didSelectRow) {
        self.didSelectRow(indexPath.row);
    }
}



- (void)reload {
    [self.oneTableView reloadData];
}


@end
